package com.example.easyutils.util;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.easyutils.R;
import com.example.easyutils.watermark.Vendor;

import java.util.List;
import java.util.Map;

//recycleView
public class MyRecycleViewAdapter extends RecyclerView.Adapter {

    private List list;
    private Context context;
    public MyRecycleViewAdapter(List mList, Context context) {
        this.list = mList;
        this.context = context;
    }


    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.clear_watermark_item,
                parent,false);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if(holder instanceof ViewHolder) {
            final String text = ((Map)list.get(position)).get("text").toString();
            ((ViewHolder) holder).mTextView.setText(text);
            ((ViewHolder) holder).mImageView.setImageResource((Integer) ((Map)list.get(position)).get("image"));
            ((ViewHolder) holder).mlinearLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Log.d("testContext",v.getContext()+"");
                    //点击事件
                    Intent intent = new Intent(context, Vendor.class);
                    intent.putExtra("Vendor",text);
                    context.startActivity(intent);
//                    context.startActivity(intent);
//                    startActivity(intent);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    //自定义的ViewHolder，持有每个Item的的所有界面元素
    class ViewHolder extends RecyclerView.ViewHolder {
        public TextView mTextView;
        public ImageView mImageView;
        public LinearLayout mlinearLayout;
        public ViewHolder(View view) {
            super(view);
            mTextView = view.findViewById(R.id.verTextView);
            mImageView = view.findViewById(R.id.verImage);
            mlinearLayout = view.findViewById(R.id.mLinearLayout);
        }
    }
}
